package com.pencilisland.pay.mgt.console.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pencilisland.pay.mgt.console.bean.JstreeNode;
import com.pencilisland.pay.mgt.console.entity.SysOrg;
import com.pencilisland.pay.mgt.console.entity.SysUser;
import com.pencilisland.pay.mgt.console.repository.OrgRepository;
import com.pencilisland.pay.mgt.console.repository.UserRepository;
import com.pencilisland.pay.mgt.console.service.OrgService;
import com.pencilisland.pay.mgt.util.G;

@Component
public class OrgServiceImpl implements OrgService {

	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	OrgRepository orgRepo;
	@Autowired
	UserRepository userRepo;

	@Override
	public JstreeNode orgTreeData(Long rootId) {
		log.info("获取机构树数据【{}】", rootId);
		SysOrg org = orgRepo.findOne(rootId);
		if (org == null) {
			log.info("机构信息不存在");
			return null;
		}

		JstreeNode root = new JstreeNode();
		root.setId(G.str(org.getOrgId()));
		root.setText(org.getOrgName());
		JstreeNode.State state = new JstreeNode.State();
		root.setState(state);
		root.setLiAttr(org);

		orgTreeDataChilds(root);

		return root;
	}

	void orgTreeDataChilds(JstreeNode root) {
		List<SysOrg> childOrgs = orgRepo.findByOrgParentOrderByOrgWeightAsc(G.l(root.getId()));

		if (childOrgs != null && !childOrgs.isEmpty()) {
			root.getState().setOpened(true);
			root.setChildren(new ArrayList<JstreeNode>());
			for (SysOrg org : childOrgs) {
				JstreeNode node = new JstreeNode();
				node.setId(G.str(org.getOrgId()));
				node.setText(org.getOrgName());
				JstreeNode.State state = new JstreeNode.State();
				node.setState(state);
				node.setLiAttr(org);

				orgTreeDataChilds(node);

				root.getChildren().add(node);
			}
		} else {
			root.getState().setOpened(false);
		}
	}

	@Override
	public SysOrg save(SysOrg org) {
		log.debug("保存机构数据");
		return orgRepo.save(org);
	}

	@Override
	public SysOrg loadData(Long id) {
		return orgRepo.findOne(id);
	}

	@Override
	public void delete(Long id) {
		log.debug("删除机构数据");
		orgRepo.delete(id);
	}

	@Override
	public JstreeNode orgTreeDataUser(Long rootId) {

		log.info("获取机构树数据【{}】", rootId);
		SysOrg org = orgRepo.findOne(rootId);
		if (org == null) {
			log.info("机构信息不存在");
			return null;
		}

		JstreeNode root = new JstreeNode();
		root.setId("G"+G.str(org.getOrgId()));
		root.setText(org.getOrgName());
		JstreeNode.State state = new JstreeNode.State();
		state.setDisabled(true);
		root.setState(state);
		root.setLiAttr(org);

		orgTreeDataUserChilds(root);

		return root;
	
	}
	
	void orgTreeDataUserChilds(JstreeNode root) {
		SysOrg rootOrg = (SysOrg)root.getLiAttr();
		List<SysOrg> childOrgs = orgRepo.findByOrgParentOrderByOrgWeightAsc(rootOrg.getOrgId());

		if (childOrgs != null && !childOrgs.isEmpty()) {
			root.getState().setOpened(true);
			root.setChildren(new ArrayList<JstreeNode>());
			for (SysOrg org : childOrgs) {
				JstreeNode node = new JstreeNode();
				node.setId("G"+G.str(org.getOrgId()));
				node.setText(org.getOrgName());
				JstreeNode.State state = new JstreeNode.State();
				state.setDisabled(true);
				node.setState(state);
				node.setLiAttr(org);

				orgTreeDataUserChilds(node);

				root.getChildren().add(node);
			}
		} else {
			root.getState().setOpened(false);
		}
		
		//新增用户
		List<SysUser> childUsers = userRepo.findByOrgIdAndStatus(rootOrg.getOrgId(),"0");
		if(childUsers!=null&&!childUsers.isEmpty()){
			root.getState().setOpened(true);
			if(root.getChildren()==null){
				root.setChildren(new ArrayList<JstreeNode>());
			}
			for(SysUser user:childUsers){
				JstreeNode node = new JstreeNode();
				node.setId(G.str(user.getUserId()));
				node.setText(user.getUserName());
				JstreeNode.State state = new JstreeNode.State();
				node.setState(state);
				node.setLiAttr(user);
				root.getChildren().add(node);
			}
		}
	}

	@Override
	public JstreeNode orgTreeDataUser1(Long rootId) {


		log.info("获取机构树数据【{}】", rootId);
		SysOrg org = orgRepo.findOne(rootId);
		if (org == null) {
			log.info("机构信息不存在");
			return null;
		}

		JstreeNode root = new JstreeNode();
		root.setId("G"+G.str(org.getOrgId()));
		root.setText(org.getOrgName());
		JstreeNode.State state = new JstreeNode.State();
		state.setDisabled(true);
		root.setState(state);
		root.setLiAttr(org);

		orgTreeDataUserChilds1(root);

		return root;
	}
	
	void orgTreeDataUserChilds1(JstreeNode root) {
		SysOrg rootOrg = (SysOrg)root.getLiAttr();
		List<SysOrg> childOrgs = orgRepo.findByOrgParentOrderByOrgWeightAsc(rootOrg.getOrgId());

		if (childOrgs != null && !childOrgs.isEmpty()) {
			root.getState().setOpened(true);
			root.setChildren(new ArrayList<JstreeNode>());
			for (SysOrg org : childOrgs) {
				JstreeNode node = new JstreeNode();
				node.setId("G"+G.str(org.getOrgId()));
				node.setText(org.getOrgName());
				JstreeNode.State state = new JstreeNode.State();
				node.setState(state);
				node.setLiAttr(org);

				orgTreeDataUserChilds1(node);

				root.getChildren().add(node);
			}
		} else {
			root.getState().setOpened(false);
		}
		
		//新增用户
		List<SysUser> childUsers = userRepo.findByOrgIdAndStatus(rootOrg.getOrgId(),"0");
		if(childUsers!=null&&!childUsers.isEmpty()){
			root.getState().setOpened(true);
			if(root.getChildren()==null){
				root.setChildren(new ArrayList<JstreeNode>());
			}
			for(SysUser user:childUsers){
				JstreeNode node = new JstreeNode();
				node.setId("U"+G.str(user.getUserId()));
				node.setText(user.getUserName());
				node.setIcon("fa fa-user");
				JstreeNode.State state = new JstreeNode.State();
				node.setState(state);
				node.setLiAttr(user);
				root.getChildren().add(node);
			}
		}
	}

	@Override
	public List<SysOrg> findAllParentOrg(SysUser user) {
		List<SysOrg> sysOrgList = new ArrayList<SysOrg>();
		SysOrg parent = orgRepo.findOne(user.getOrgId());
		while(parent.getOrgParent()!=null){
			
			sysOrgList.add(parent);
			parent = orgRepo.findOne(parent.getOrgParent());
		}
		
		return sysOrgList;
	}

	@Override
	public SysOrg findByOrgNumber(String orgNumber) {
		return orgRepo.findByOrgNumber(orgNumber);
	}

}
